<template>
    <div>
        <div class="top_header">
            <wd-header title="课程选择">
                <router-link to="/" slot="left">
                    <span class="iconfont">&#xe697;</span>
                </router-link>
            </wd-header>
        </div>
        
        <div class="select">
            <select v-model="grade" @change="selectresult">
                <option value="0">年级</option>
                <option v-for="(item,index) in gradelist" :value="item.gradeId">{{item.grade_name}}</option>
            </select>
            <select v-model="semester" @change="selectresult">
                <option value="0">学期</option>
                <option v-for="(item,index) in semesterlist" :value="item.semesterId">{{item.semesterName}}</option>
            </select>
            <select v-model="version" @change="selectresult">
                <option value="0">版本</option>
                <option v-for="(item,index) in versionlist" :value="item.class_type">{{item.class_name}}</option>
            </select>
        </div>
        <div class="versionList">
            <transition-group enter-active-class="animated fadeIn">
                <router-link tag="div" :to="{name:'singlebook',params:{
                    classid:item.classId,
                    title:item.grade_name,
                    payid:item.classId,
                    imgurl:item.imgurl,
                    content:item.class_name,
                    paytype:item.payType,
                    totals:item.total_lessons
                } }" class="oneOf" v-for="(item,index) in lists" :key="index">
                    <div class="top">
                        <div class="left">
                            <img v-lazy="item.imgurl" alt="">
                        </div>
                        <div class="right">
                            <p>{{item.class_name}}</p>
                            <span>{{item.grade_name}}</span>
                        </div>
                    </div>
                    <div class="bottom">
                        <div class="bottomLeft">
                            <span class="many">{{item.orders}}</span>人学习
                        </div>
                        <div class="bottomRight">
                            共{{item.total_lessons}}单元
                        </div>
                    </div>
                </router-link>
            </transition-group>
        </div>
    </div>
</template>
<script>
export default {
    name: 'serieswords',
    data() {
        return {
            topdatas: [],
            lists: [],
            grade: 0,
            semester: 0,
            version: 0,
            commonurl: 'http://www.jydsapp.com/jydsApi/api/h5/study/getWordIndex',
            selecturl: 'http://www.jydsapp.com/jydsApi/api/h5/study/getWodeBook',
            auth: JSON.parse(localStorage.user)
        }
    },
    computed: {
        gradelist() {
            return this.topdatas.gradeList
        },
        semesterlist() {
            return this.topdatas.getSemesterList
        },
        versionlist() {
            return this.topdatas.versionNameList
        },
        detaillists() {
            return this.topdatas.versionList
        }
    },
    watch:{
        grade(){
            window.scrollTo(0,0)
        },
        semester(){
            window.scrollTo(0,0)
        },
        version(){
            window.scrollTo(0,0)
        }
    },
    created() {
        this.getCommonData()
    },
    beforeRouteLeave(to, from, next) {
        let routeinfo = to.params // 从主页或单词主页到列表页和购买页的数据
        this.$store.commit('saveWordIndexToSingleBookOrBuyData', routeinfo)
        next()
    },
    methods: {
        selectresult() {
            let strgrade = this.grade == 0 ? `` : `"gradeId":${this.grade}`
            let strsemester = this.semester == 0 ? `` : `"semesterId":${this.semester}`
            let strversion = this.version == 0 ? `` : `"version":${this.version}`
            let str = `{"userPhone":"${this.auth.user.phoneNum}",
                        "token":"${this.auth.token}",
                        ${strgrade},
                        ${strsemester},
                        ${strversion},
                        "selectType":0}`
            this.fetch({
                method: 'post',
                url: this.selecturl,
                params: {
                    "paramStr": str
                }
            }).then((data) => {
                let res = data.data
                if (res.code == 200) {
                    this.lists = JSON.parse(res.data).versionList
                } else if (res.code == 103) { //五天内未登录 token失效跳转到登陆页面
                    localStorage.removeItem('user')
                    this.$Toast({
                        content: '请重新登录'
                    })
                    this.$router.replace('/login')
                }  else {
                    this.$Toast({
                        content: res.message,
                    })
                }
            }).catch((err) => {
                console.log(err)
            })
        },
        getCommonData() {
            let str = `{"userPhone":"${this.auth.user.phoneNum}","token":"${this.auth.token}","grade_type":"1"}`
            let toast = this.$Toast({
                content: '加载中…',
                iconClass: 'loader',
                autoClose:false
            })
            this.fetch({
                method: 'post',
                url: this.commonurl,
                params: {
                    "paramStr": str
                }
            }).then((data) => {
                let res = data.data
                if (res.code == 200) {
                    toast.close()
                    let temp = JSON.parse(res.data)
                    this.topdatas = temp
                    this.lists = temp.versionList
                } else if (res.code == 103) { //五天内未登录 token失效跳转到登陆页面
                    toast.close()
                    localStorage.removeItem('user')
                    this.$Toast({content: '请重新登录'})
                    this.$router.replace('/login')
                }  else {
                    toast.close()
                    this.$Toast({content: res.message})
                }
            }).catch((err) => {
                
            })
        }
    }
}
</script>
<style lang="scss" scoped>
.animated {
    animation-duration: .5s;
}
.top_header{
    position:fixed;
    width: 100%;
    top: 0;
    left: 0;
}
.select {
    display: flex;
    position: fixed;
    top: 1.296296rem;
    left: 0;
    width: 100%;
    background: #fff;
    select {
        appearance:none;
        height: 1.2rem;
        line-height: 1.2rem;
        flex: 1;
        padding-left: .5rem;
        outline: 0;
        border: 0;
        background: url("http://ourjs.github.io/static/2015/arrow.png") no-repeat scroll 95% center transparent;
        &:nth-child(2) {
            border-left: 1px solid #eee;
            border-right: 1px solid #eee;
        }
        option {
            border: 0;
            outline: 0;
            padding: 0.185185rem;
            text-align: center;
            margin: 0.185185rem;
        }
    }
}

.versionList {
    margin-top: 2.496296rem;
    background: #eee;
    padding: 0.37037rem 0.185185rem;
    .oneOf {
        background: #fff;
        padding: 0.277778rem;
        margin-bottom: 0.277778rem;
        .top {
            display: flex;
            justify-content: space-between;
            border-bottom: 1px solid #eee;
            padding-bottom: 0.277778rem;
            .left {
                flex: 1;
                img {
                    width: 100%;
                }
            }
            .right {
                flex: 1;
                margin-left: 0.37037rem;
                p {
                    font-size: 0.444444rem;
                    color: #333;
                    margin: 0.277778rem 0 0.37037rem 0;
                }
                span {
                    color: #ff9920;
                }
            }
        }
        .bottom {
            display: flex;
            justify-content: space-between;
            padding-top: 0.185185rem;
            color: #333;
            .bottomLeft {
                .many {
                    color: #fe4f4a;
                }
            }
        }
    }
}
</style>
